package retrofit.boot.hystrix;

import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
import com.netflix.hystrix.strategy.HystrixPlugins;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit.boot.hystrix.strategy.RetrofitHystrixConcurrencyStrategy;
import retrofit.boot.hystrix.strategy.RetrofitHystrixPropertiesStrategy;

/**
 * Created by wulg on 2019/10/22
 * description:hystrix默认配置
 */
@Configuration
//@ConditionalOnProperty(name="retrofit.hystrix.enabled",havingValue = "true")
@EnableConfigurationProperties({HystrixProperties.class})
public class HystrixAutoConfiguration {

    private final HystrixProperties properties;

    //URLConfigurationSource
    public HystrixAutoConfiguration(HystrixProperties properties) {
        this.properties = properties;
        System.setProperty("archaius.configurationSource.defaultFileName",properties.getDefaultFileName());
        System.setProperties(properties.getHystrixCommand());
        System.setProperties(properties.getHystrixThreadpool());
        HystrixPlugins.reset();
        HystrixPlugins.getInstance().registerConcurrencyStrategy(new RetrofitHystrixConcurrencyStrategy());
        HystrixPlugins.getInstance().registerPropertiesStrategy(new RetrofitHystrixPropertiesStrategy());
    }

    @Bean
    public HystrixCommandAspect hystrixCommandAspect() {
        return new HystrixCommandAspect();
    }
}
